﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace bytes与struct的序列化
{
    class Program
    {
        static void Main(string[] args)
        {
            TopUPMachineProtocol tp = new TopUPMachineProtocol();
            tp.frameHeader = 0x55;

            tp.computerMacAddr = new byte[6];
            for (int i = 0; i < 6; i++) tp.computerMacAddr[i] = (byte)(0xA9+i);

            tp.machineMacAddr = new byte[6];
            for (int i = 0; i < 6; i++) tp.machineMacAddr[i] = (byte)(0xB9 + i);

            tp.machineType = 0xFF;

            tp.sourceMachineNum = 0x1234;

            tp.CmdFlag = 0x04;

            tp.dataLen = 0x5678;

            tp.targetMachineNum = 0xABCD;

            tp.systemTime = 0x12345678;

            tp.heartbeatInterval = 0x0123;

            tp.heartbeatAckDly = 0x4567;

            tp.addCRC = 0xCC;

            tp.exclusiveOrCRC = 0xDD;

            tp.frameTail = 0x5E;


            Console.WriteLine("tp.frameHeader=" + tp.frameHeader.ToString("X2"));

            Console.Write("tp.computerMacAddr=");
            foreach (byte b in tp.computerMacAddr)
                Console.Write(b.ToString("X2"));
            Console.WriteLine();

            Console.Write("tp.machineMacAddr=");
            foreach (byte b in tp.machineMacAddr)
                Console.Write(b.ToString("X2"));
            Console.WriteLine();

            Console.WriteLine("tp.machineType=" + tp.machineType.ToString("X2"));
            Console.WriteLine("tp.sourceMachineNum=" + tp.sourceMachineNum.ToString("X4"));
            tp.sourceMachineNum = CSerialization.ByteCodeOrderConversion(tp.sourceMachineNum);
            Console.WriteLine("tp.CmdFlag=" + tp.CmdFlag.ToString("X2"));
            Console.WriteLine("tp.dataLen=" + tp.dataLen.ToString("X4"));
            tp.dataLen = CSerialization.ByteCodeOrderConversion(tp.dataLen);
            Console.WriteLine("tp.targetMachineNum=" + tp.targetMachineNum.ToString("X4"));
            tp.targetMachineNum = CSerialization.ByteCodeOrderConversion(tp.targetMachineNum);            
            Console.WriteLine("tp.systemTime=" + tp.systemTime.ToString("X4"));
            tp.systemTime = CSerialization.ByteCodeOrderConversion(tp.systemTime);
            Console.WriteLine("tp.heartbeatInterval=" + tp.heartbeatInterval.ToString("X4"));
            tp.heartbeatInterval = CSerialization.ByteCodeOrderConversion(tp.heartbeatInterval);
            Console.WriteLine("tp.heartbeatAckDly=" + tp.heartbeatAckDly.ToString("X4"));
            tp.heartbeatAckDly = CSerialization.ByteCodeOrderConversion(tp.heartbeatAckDly);
            Console.WriteLine("tp.addCRC=" + tp.addCRC.ToString("X2"));
            Console.WriteLine("tp.exclusiveOrCRC=" + tp.exclusiveOrCRC.ToString("X2"));
            Console.WriteLine("tp.frameTail=" + tp.frameTail.ToString("X2"));


            //序列化成数组
            byte[] buffers = CSerialization.StructToBytes(tp);

            Console.WriteLine("*******************************************");

            foreach (byte b in buffers) {
                Console.Write(b.ToString("X2") + " ");
            }
            Console.WriteLine();

            TopUPMachineProtocol tp1 = (TopUPMachineProtocol)CSerialization.BytesToStuct(buffers, typeof(TopUPMachineProtocol));

            Console.WriteLine("*******************************************");

            Console.WriteLine("tp1.frameHeader=" + tp1.frameHeader.ToString("X2"));

            Console.Write("tp1.computerMacAddr=");
            foreach (byte b in tp1.computerMacAddr)
                Console.Write(b.ToString("X2"));
            Console.WriteLine();

            Console.Write("tp1.machineMacAddr=");
            foreach (byte b in tp1.machineMacAddr)
                Console.Write(b.ToString("X2"));
            Console.WriteLine();

            Console.WriteLine("tp1.machineType=" + tp1.machineType.ToString("X2"));
            tp1.sourceMachineNum = CSerialization.ByteCodeOrderConversion(tp1.sourceMachineNum);
            Console.WriteLine("tp1.sourceMachineNum=" + tp1.sourceMachineNum.ToString("X4"));
            Console.WriteLine("tp1.CmdFlag=" + tp1.CmdFlag.ToString("X2"));
            tp1.dataLen = CSerialization.ByteCodeOrderConversion(tp1.dataLen);
            Console.WriteLine("tp1.dataLen=" + tp1.dataLen.ToString("X4"));
            tp1.targetMachineNum = CSerialization.ByteCodeOrderConversion(tp1.targetMachineNum);
            Console.WriteLine("tp1.targetMachineNum=" + tp1.targetMachineNum.ToString("X4"));
            tp1.systemTime = CSerialization.ByteCodeOrderConversion(tp1.systemTime);
            Console.WriteLine("tp1.systemTime=" + tp1.systemTime.ToString("X4"));
            tp1.heartbeatInterval = CSerialization.ByteCodeOrderConversion(tp1.heartbeatInterval);
            Console.WriteLine("tp1.heartbeatInterval=" + tp1.heartbeatInterval.ToString("X4"));
            tp1.heartbeatAckDly = CSerialization.ByteCodeOrderConversion(tp1.heartbeatAckDly);
            Console.WriteLine("tp1.heartbeatAckDly=" + tp1.heartbeatAckDly.ToString("X4"));
            Console.WriteLine("tp1.addCRC=" + tp1.addCRC.ToString("X2"));
            Console.WriteLine("tp1.exclusiveOrCRC=" + tp1.exclusiveOrCRC.ToString("X2"));
            Console.WriteLine("tp1.frameTail=" + tp1.frameTail.ToString("X2"));

            Console.ReadLine();

        }
    }
}
